package ifi.p17.jarmanagement.model;

import java.util.StringTokenizer;

public class Purchase {

	public static final int TYPE_NULL = -1;
	public static final int TYPE_NORMAL = 0;
	public static final int TYPE_DISPLAY_DAY = 1;
	public static final int TYPE_DISPLAY_MONTH = 2;
	
	String jar;
	String time;
	String unit;
	int value;
	String description;
	boolean checked;
	int type;
	int remaining;
	
	public Purchase(){
		this.type = TYPE_NULL;
	}
	
	public Purchase(String jar, String time, int value, String unit,String des) {
		// for display on one day
		this.type = TYPE_NORMAL;
		this.jar = jar;
		this.time = time;
		this.value = value;
		this.description = des;
		this.unit = unit;
	}

	public Purchase(String time, int value){
		// for display on day in month
		this.type = TYPE_DISPLAY_DAY;
		this.time = time;
		this.value = value;
	}
	
	public Purchase(String time, int value, int remaining){
		// for display on day in month
		this.type = TYPE_DISPLAY_MONTH;
		this.time = time;
		this.value = value;
		this.remaining = remaining;
	}

	public void setNormalTypeInfo(String jar, String time, int value, String unit,String des){
		this.type = TYPE_NORMAL;
		this.jar = jar;
		this.time = time;
		this.value = value;
		this.description = des;
		this.unit = unit;
	}
	
	public void loadFromString(String line){
		this.type = TYPE_NORMAL;
		StringTokenizer st = new StringTokenizer(line, "@");
		jar = st.nextToken();
		time = st.nextToken();
		value = Integer.parseInt(st.nextToken());
		unit = st.nextToken();
		description = st.hasMoreElements() ? st.nextToken() : "" ;
	}
	
	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getJar() {
		return jar;
	}

	public void setJar(String jar) {
		this.jar = jar;
	}

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	@Override
	public String toString() {
		return jar + "@" + getTime() + "@" + Integer.toString(getValue()) + "@" +unit + "@"+ getDescription();
	}

	public String infoLine1(){
		if (type == TYPE_NORMAL){
			return getTime() + " " + getValue() + "("+ getUnit() +")";
		} else if (type == TYPE_DISPLAY_DAY){
			return "Day: " + time;
		} else if (type == TYPE_DISPLAY_MONTH){
			return "Month: " + time;
		}
		
		return "";
	}
	
	public String infoLine2(){
		if (type == TYPE_NORMAL){
			return getJar() + ": " + getDescription();
		} else if (type == TYPE_DISPLAY_DAY){
			return "Depense: " + Integer.toString(value);
		} else if (type == TYPE_DISPLAY_MONTH){
			return "Revenu: " + value + " - Remaining: " + remaining;
		}
		
		return "";

		
	}

}
